package com.sjd.mall.service.account.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.sjd.common.constant.IntegralConstants;
import com.sjd.common.constant.MemberLevelConstants;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.utils.StringUtils;
import com.sjd.mall.domain.SjdAccountDO;
import com.sjd.mall.domain.SjdIntegralDetailDO;
import com.sjd.mall.domain.dto.account.BasePageHelperDTO;
import com.sjd.mall.domain.dto.account.InsertIntegralRequestDTO;
import com.sjd.mall.domain.dto.account.WxIntergralDetailResponseDTO;
import com.sjd.mall.mapper.SjdAccountMapper;
import com.sjd.mall.mapper.SjdIntegralDetailMapper;
import com.sjd.mall.service.account.AccountIntegralService;
import com.sjd.mall.service.account.AccountService;
import com.sjd.wx.utils.ThirdSessionHolder;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.tuple.MutablePair;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: sjd
 * @Package: com.sjd.mall.service.account.impl
 * @ClassName: AccountIntegralServiceImpl
 * @Author: genglong
 * @Description:
 * @Date: 2022/1/18 14:32
 * @Version: 1.0
 */
@Service
@AllArgsConstructor
public class AccountIntegralServiceImpl implements AccountIntegralService {

    private final SjdAccountMapper sjdAccountMapper;

    private final SjdIntegralDetailMapper sjdIntegralDetailMapper;

    @Override
    public AjaxResult queryClockStatus() {
        int clockNum = sjdIntegralDetailMapper.queryClockStatus(ThirdSessionHolder.getThirdSession().getAccountId());
        MutablePair<String,Integer> mutablePair = new MutablePair<>("status", 0);
        if(clockNum==0){
            return AjaxResult.success(mutablePair);
        }
        mutablePair.setRight(1);
        return AjaxResult.success(mutablePair);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertIntegralDetail(InsertIntegralRequestDTO requestDTO) {
        SjdAccountDO sjdAccountDO = sjdAccountMapper.selectByPrimaryKey(ThirdSessionHolder.getThirdSession().getAccountId());
        if (ObjectUtil.isEmpty(sjdAccountDO)){
            return AjaxResult.error("账户不存在！");
        }
        //打卡获取积分
        if(IntegralConstants.CLOCK.equals(requestDTO.getIntergralType())){
            if(StringUtils.isEmpty(requestDTO.getImageUrl())){
                return AjaxResult.error("打卡图片未上传！");
            }
            SjdIntegralDetailDO sjdIntegralDetailDO = SjdIntegralDetailDO.builder().integralType(requestDTO.getIntergralType())
                    .accountId(sjdAccountDO.getId()).memberId(sjdAccountDO.getMemberId())
                    .imageUrl(requestDTO.getImageUrl()).description("打卡成功！").modifyTime(new Date()).createTime(new Date()).build();
            int clockNum = sjdIntegralDetailMapper.queryIntegrlClockNumByAccountId(ThirdSessionHolder.getThirdSession().getAccountId());
            if(clockNum<3){
                Integer integral = sjdAccountDO.getIntegral() + requestDTO.getIntegralNum();
                sjdAccountMapper.updateByPrimaryKeySelective(SjdAccountDO.builder().id(sjdAccountDO.getId()).integral(integral)
                        .modifyTime(new Date()).build());
                sjdIntegralDetailDO.setIntegralNum(requestDTO.getIntegralNum());
                sjdIntegralDetailDO.setDescription("打卡成功,并获得"+requestDTO.getIntegralNum()+"积分");
            }else {
                sjdIntegralDetailDO.setIntegralNum(0);
                sjdIntegralDetailDO.setDescription("打卡成功,本周打卡次数超过3次，未获得积分！");
            }


            sjdIntegralDetailMapper.insert(sjdIntegralDetailDO);
        }


        //消费积分-升级土地
        if(IntegralConstants.CONSUME.equals(requestDTO.getIntergralType())){
            if(requestDTO.getIntegralNum() > sjdAccountDO.getIntegral()){
                return AjaxResult.error("积分不足，无法购买土地");
            }
            Integer integral = sjdAccountDO.getIntegral() - requestDTO.getIntegralNum();
            sjdAccountMapper.updateByPrimaryKeySelective(SjdAccountDO.builder().id(sjdAccountDO.getId()).integral(integral)
                    .memberLevel(MemberLevelConstants.VIP).modifyTime(new Date()).build());
            SjdIntegralDetailDO sjdIntegralDetailDO = SjdIntegralDetailDO.builder().integralType(requestDTO.getIntergralType())
                    .integralNum(requestDTO.getIntegralNum()).accountId(sjdAccountDO.getId()).memberId(sjdAccountDO.getMemberId())
                    .description(requestDTO.getDescription()).modifyTime(new Date()).createTime(new Date()).build();
            sjdIntegralDetailMapper.insert(sjdIntegralDetailDO);
        }
        return AjaxResult.success();
    }

    @Override
    public List<WxIntergralDetailResponseDTO> queryIntegrallist() {
        List<SjdIntegralDetailDO> list = sjdIntegralDetailMapper.queryIntegrlListByAccountId(ThirdSessionHolder.getThirdSession().getAccountId());
        List<WxIntergralDetailResponseDTO> responseList = new ArrayList<>();
        for (SjdIntegralDetailDO sjdIntegralDetailDO : list){
            WxIntergralDetailResponseDTO responseDTO = new WxIntergralDetailResponseDTO();
            responseDTO.setId(sjdIntegralDetailDO.getId());
            responseDTO.setAccountId(sjdIntegralDetailDO.getAccountId());
            responseDTO.setMemberId(sjdIntegralDetailDO.getMemberId());
            responseDTO.setIntegralNum(sjdIntegralDetailDO.getIntegralNum());
            responseDTO.setIntegralType(sjdIntegralDetailDO.getIntegralType());
            responseDTO.setDescription(sjdIntegralDetailDO.getDescription());
            if(IntegralConstants.CLOCK.equals(sjdIntegralDetailDO.getIntegralType())){
                responseDTO.setImageUrl(sjdIntegralDetailDO.getImageUrl());
            }
            responseDTO.setCreateTime(sjdIntegralDetailDO.getCreateTime());
            responseDTO.setModifyTime(sjdIntegralDetailDO.getModifyTime());
            responseList.add(responseDTO);
        }
        return responseList;
    }
}
